package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    private CityInfoMapper cityInfoMapper;
    @Autowired
    private DistrictInfoMapper districtInfoMapper;
    @Override
    public IPage<ApartmentItemVo> getListApartmentItemVo(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> ipage = apartmentInfoMapper.getListApartmentItemVo(page,queryVo);
        System.out.println(ipage.toString());
        return ipage;
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVoById(Long id) {
        //ApartmentDetailVo需要ApartmentInfo  List<GraphVo> graphVoList  List<LabelInfo>labelInfoList List<FacilityInfo> facilityInfoList
        // List<FeeValueVo>feeValueVoList 五种数据进行填充
        //其中ApartmentInfo可以直接通过id进行查询 其他4种数据都需要通过id在中间表中查询
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectOne(new LambdaQueryWrapper<ApartmentInfo>().eq(ApartmentInfo::getId, id));
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        List<GraphVo> graphVoList = graphInfoMapper.selectListByApartmentId(id);
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void saveOrUpdateapartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
        //ApartmentSubmitVo中包含ApartmentInfo List<Long> facilityInfoIds;
        // List<Long> labelIds; List<Long> feeValueIds;List<GraphVo> graphVoList;
        //五个数据
        //需要判断此数据是保存还是更新 通过获取apartmentSubmitVo中的id进行判断
        //存在则直接进行删除对应公寓id的中间表数据 如何在将此次携带的中间表数据进行添加
        //不存在则先进行保存数据后执行中间表数据添加操作
        //注：mybatisplus可以在进行新增操作后将新增数据的id值返回到数据中
        Long id = apartmentSubmitVo.getId();
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId());
        CityInfo cityInfo = cityInfoMapper.selectById(apartmentSubmitVo.getCityId());
        DistrictInfo districtInfo = districtInfoMapper.selectById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        this.saveOrUpdate(apartmentSubmitVo);
        if(id!= null){
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType, ItemType.APARTMENT));
        }
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        Long newid = apartmentSubmitVo.getId();
        facilityInfoIds.forEach(facilityInfoId -> {
            ApartmentFacility apartmentFacility = ApartmentFacility.builder().facilityId(facilityInfoId).apartmentId(newid).build();
            apartmentFacilities.add(apartmentFacility);
        });
        apartmentFacilityService.saveBatch(apartmentFacilities);
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> apartmentLabels = new ArrayList<>();
        labelIds.forEach(labelId -> {
            ApartmentLabel apartmentLabel = ApartmentLabel.builder().labelId(labelId).apartmentId(newid).build();
            apartmentLabels.add(apartmentLabel);
        });
        apartmentLabelService.saveBatch(apartmentLabels);
        List<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        apartmentSubmitVo.getFeeValueIds().forEach(feeValueId -> {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().feeValueId(feeValueId).apartmentId(newid).build();
            apartmentFeeValues.add(apartmentFeeValue);
        });
        apartmentFeeValueService.saveBatch(apartmentFeeValues);
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfos = new ArrayList<>();
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new  GraphInfo();
            graphInfo.setItemId(newid);
            graphInfo.setName(graphVo.getName());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setUrl(graphVo.getUrl());
            graphInfos.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfos);
    }

    @Override
    public void removeByApartmentId(Long id) {
        this.remove(new LambdaQueryWrapper<ApartmentInfo>().eq(ApartmentInfo::getId,id));
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType, ItemType.APARTMENT));
    }
}




